import Vue from 'vue'
import Vuex from 'vuex'
import {INCREMENT,INCREMENTBYCOUNT,INCREMENTBYPARAM} from  '../components/store/cons-type'
// 引入使用插件
Vue.use(Vuex)

//创建对象
const store = new Vuex.Store({
  state: {
    counter: 1000,
    num: 100,
  },
  mutations: {
    [INCREMENT](state) {
      state.counter++;
    },
    [INCREMENTBYCOUNT](state,i) {
      state.counter += i;
    },
    incrementByParam(state,a) {
      state.counter += a.count;
      a.success('我也支持回调')
    },
    /*错误异步操作*/
    incrementByAsync(state) {
      setTimeout(function () {
        state.counter++;
      },2000)
    }
  },
  getters: {
    sumCounterAndNum(state){
     return state.counter+ state.num;
    },
    sum2CounterAndNum(state,getters){
      // 调用其他getters 函数
      return getters.sumCounterAndNum+ state.num;
    },
    sum3CounterAndNum(state,getters){
      // 返回一个函数,且带参
      return function (i) {
        return  getters.sumCounterAndNum+ state.num + i;
      }
    }
  },
  actions: {
    incrementByAsync(context) {
      setTimeout(function () {
        context.commit(INCREMENT)
      },2000)
    }
  },
  moudels: {

  }
})

// 导出 store
export default store
